﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace CPPEI.Coolzon.Tools.LaserPoint
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        NamedPipeServerStream _pipeServer;
        public StreamWriter _pipeClientWriter;
        NamedPipeClientStream _pipeClient;
        MainWindow _mainWindow;
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            _mainWindow = new MainWindow();
            _mainWindow.Show();

            string[] args = e.Args;
            string pipeName = "LaserPoint";


            try
            {
                _pipeServer = new NamedPipeServerStream(string.Format("{0}DA", pipeName),
                    PipeDirection.In,
                    1,
                    PipeTransmissionMode.Message,
                    PipeOptions.Asynchronous);
                ThreadPool.QueueUserWorkItem(PipeRunning);
            }
            catch
            {
                _pipeServer = null;
            }
        }

        #region pipe server running
        private void PipeRunning(object state)
        {
            if (this._pipeServer == null)
            {
                return;
            }
            this._pipeServer.BeginWaitForConnection((o) =>
            {
                NamedPipeServerStream server = (NamedPipeServerStream)o.AsyncState;
                server.EndWaitForConnection(o);
                StreamReader sr = new StreamReader(server);
                string result = null;
                string clientName = server.GetImpersonationUserName();
                while (true)
                {
                    result = sr.ReadLine();
                    if (result == null || string.Equals(result, "EXIT", StringComparison.CurrentCultureIgnoreCase))
                    {
                        _mainWindow.CloseLP();
                        break;
                    }

                    //cmd:p1,p2,p3
                    string[] ret = result.Split(new string[] { ":>" }, StringSplitOptions.None);
                    if (ret != null && ret.Length > 1)
                    {
                        switch (ret[0].ToUpper())
                        {
                            case "SHOW":
                                string[] strPt = ret[1].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
                                Show(Convert.ToDouble(strPt[0]), Convert.ToDouble(strPt[1]));
                                break;
                            case "HIDE":
                                Hide();
                                break;
                        }
                    }
                }
                sr.Close();
                _pipeServer.Close();
                _pipeServer = null;
            }, this._pipeServer);
        }
        #endregion

        private void Show(double x, double y)
        {
            _mainWindow.ShowLP(x, y);
        }

        private void Hide()
        {
            _mainWindow.HideLP();
        }
    }
}
